package com.yoomiito.app.ui.yijiayou

import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import android.text.Spannable
import android.text.SpannableString
import android.text.style.RelativeSizeSpan
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.yoomiito.app.R
import com.yoomiito.app.distanceFormat
import com.yoomiito.app.model.oil.OilStationData
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.utils.SpannableUtils
import com.yoomiito.app.widget.CouponView

class GasStationListAdapter(data:List<OilStationData>?) :BaseQuickAdapter<OilStationData,BaseViewHolder>(R.layout.item_gas_station,data){

    override fun convert(helper: BaseViewHolder?, item: OilStationData) {
        helper?.getView<ConstraintLayout>(R.id.gasStationItem)?.let {
            val lp = it.layoutParams as androidx.recyclerview.widget.RecyclerView.LayoutParams
            val margin = DensityUtil.dp2px(12f)
            lp.leftMargin = margin
            lp.rightMargin = margin
        }
        helper?.addOnClickListener(R.id.iv)
        helper?.setText(R.id.stationName,item.gasName)
        helper?.setText(R.id.stationAddress,item.province+item.city+item.county+item.gasAddress)
        val s = "¥${item.price}"
        val spanText = SpannableString(s)
        spanText.setSpan(RelativeSizeSpan(0.5f), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        if (s.contains(".")){
            spanText.setSpan(RelativeSizeSpan(0.5f), s.length - s.indexOf("."), s.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        }
        helper?.setText(R.id.gasPrice,spanText)

        helper?.getView<CouponView>(R.id.couponView)?.let {
            it.setText("¥${item.priceGun?.toBigDecimal().minus(item.price.toBigDecimal()).toString()}")
        }
        helper?.setText(R.id.oldPrice,SpannableUtils.getSpannable( "油站价 ¥${item.priceGun}",0))
        helper?.setText(R.id.stationDistance,item.distance.toDouble().distanceFormat())
    }

}